package com.suntop.carousel.qlmediaplay.tools;




import android.util.Log;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject;

/**
 * 总线事件
 */
@SuppressWarnings("rawtypes")
public class Rxbus {
    private Rxbus() {
        compositeDisposable=new CompositeDisposable();
    }
    private static Rxbus inStance;

    public static Rxbus getInStance() {
        if (null == inStance) {
            inStance = new Rxbus();
        }
        return inStance;
    }
    CompositeDisposable compositeDisposable;
    private HashMap<String, HashMap<Object, BehaviorSubject>>  hashMap=new HashMap<>();


    /**
     * 注册事件
     * @param tag
     * @param consumer
     * @param <T>
     */
    public <T> void registerRxbus(String tag, Object c, Consumer<? super T> consumer){
        try {
            HashMap<Object, BehaviorSubject> publishSubjects = hashMap.get(tag);
            if (publishSubjects==null){
                publishSubjects=new HashMap<>();
            }
            BehaviorSubject<T> objectPublishSubject = BehaviorSubject.create();
            Disposable subscribe = objectPublishSubject.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(consumer);
            publishSubjects.put(c,objectPublishSubject);
            hashMap.put(tag,publishSubjects);
            compositeDisposable.add(subscribe);

        }catch (Exception e){
            Log.e("Rxbus", "注册RxBus 失败");
        }
    }

    /**
     * 加入
     * @param disposable
     */
    public void  addSubscribe(Disposable disposable){
        compositeDisposable.add(disposable);
    }

    /**
     * 移除
     * @param disposable
     */
    public void  removeSubscribe(Disposable disposable){
        compositeDisposable.remove(disposable);
    }
    /**
     * 取消注册
     * @param tag
     */
    public void unRegisterRxBus(String tag, Object c){
        HashMap<Object, BehaviorSubject> publishSubjects = hashMap.get(tag);
        if (publishSubjects==null){
            return;
        }
        BehaviorSubject<?> publishSubject = publishSubjects.get(c);
        if (publishSubject==null){
            return;
        }
        Disposable subscribe = publishSubject.subscribe();
        compositeDisposable.remove(subscribe);
        publishSubjects.remove(c);
        if (publishSubjects.size()==0){
            hashMap.remove(tag);
        }
    }

    /**
     * 发送事件消息
     * @param tag
     * @param o
     */
    public void post(String tag, Object o){
        HashMap<Object, BehaviorSubject> publishSubjects = hashMap.get(tag);
        if (publishSubjects==null){
            return;
        }
        Iterator<Map.Entry<Object, BehaviorSubject>> iterator = publishSubjects.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Object, BehaviorSubject> next = iterator.next();
            BehaviorSubject value = next.getValue();
            if (value!=null){
                value.onNext(o);
            }
        }
    }
    /**
     * 发送单个事件消息
     * @param tag
     * @param o
     */
    public void post(String tag, Object o, Object object){
        HashMap<Object, BehaviorSubject> publishSubjects = hashMap.get(tag);
        if (publishSubjects==null){
            return;
        }
        BehaviorSubject publishSubject = publishSubjects.get(o);
        if (publishSubject!=null) {
            publishSubject.onNext(object);
        }
    }

}
